package com.example.elysiumease;

import com.example.elysiumease.mapper.SoulPlacementMapper;
import com.example.elysiumease.mapper.SoulMapper;
import com.example.elysiumease.mapper.ZoneMapper;
import com.example.elysiumease.model.Soul;
import com.example.elysiumease.model.SoulPlacement;
import com.example.elysiumease.model.Zone;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.annotation.Commit;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
@Transactional
@Commit
public class SoulPlacementMapperTest {

    @Autowired
    private SoulPlacementMapper soulPlacementMapper;

    @Autowired
    private SoulMapper soulMapper;

    @Autowired
    private ZoneMapper zoneMapper;

    @Test
    void testInsertSoulPlacement() {
        // 插入灵魂数据
        Soul soul = soulMapper.selectById(16);

        // 插入区域数据
        Zone zone = zoneMapper.selectById(1);

        // 插入灵魂安置记录
        SoulPlacement soulPlacement = new SoulPlacement();
        soulPlacement.setSoul(soul);
        soulPlacement.setZone(zone);
        soulPlacement.setPlacementTime(LocalDateTime.now());
        soulPlacement.setExpirationTime(LocalDateTime.now().plusDays(30));

        int result = soulPlacementMapper.insertSoulPlacement(soulPlacement);
        assertEquals(1, result);
        assertNotNull(soulPlacement.getPlacementId());
    }

    @Test
    void testUpdateSoulPlacement() {
        // 插入灵魂数据
        Soul soul = soulMapper.selectById(16);

        // 插入区域数据
        Zone zone = zoneMapper.selectById(1);

        // 插入灵魂安置记录
        SoulPlacement soulPlacement = new SoulPlacement();
        soulPlacement.setSoul(soul);
        soulPlacement.setZone(zone);
        soulPlacement.setPlacementTime(LocalDateTime.now());
        soulPlacement.setExpirationTime(LocalDateTime.now().plusDays(30));
        soulPlacementMapper.insertSoulPlacement(soulPlacement);

        // 更新记录
        soulPlacement.setExpirationTime(LocalDateTime.now().plusDays(60));
        int result = soulPlacementMapper.updateSoulPlacement(soulPlacement);
        assertEquals(1, result);

        // 验证更新结果
        SoulPlacement updatedSoulPlacement = soulPlacementMapper.selectById(soulPlacement.getPlacementId());
        assertEquals(LocalDateTime.now().plusDays(60).toLocalDate(), updatedSoulPlacement.getExpirationTime().toLocalDate());
    }

}